package com.doubletick.proxy.liao;

import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

@Slf4j
public class TestTime {
    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < 3; i++) {
            new Thread(() -> {
                run1();
            }).start();
        }
        while (count.get() != 30) {
            long c = count.get();
            if (c == 0) {
                continue;
            }
            long sum = times.stream().mapToLong(Long::valueOf).sum();
            long min = times.stream().mapToLong(Long::valueOf).min().getAsLong();
            long max = times.stream().mapToLong(Long::valueOf).max().getAsLong();
            log.info("running...  count:{} 平均耗时:{} min:{} max:{}", count, (count.get() != 0 ? sum / count.get() : 0), min, max);
            Thread.sleep(1000 * 1L);
        }
        log.info("结束：{}");
    }

    //    public static String url = "http://localhost:8080/api/ip/getIpByIso?iso=br&ipProvider=16";
    public static String url = "https://pb.doubletick.site/api/ip/getIpByIso?iso=br&ipProvider=16";

    public static List<Long> times = new ArrayList<>();

    public static AtomicInteger count = new AtomicInteger(0);

    private static void run1() {
        for (int i = 0; i < 10; i++) {
            long l1 = System.currentTimeMillis();
            HttpResponse response = null;
            try {
                log.info("开始请求:{}", url);
                response = HttpUtil.createPost(url).execute();
            } finally {
                count.incrementAndGet();
                long l2 = System.currentTimeMillis();
                times.add(l2 - l1);
                if (response != null) {
                    log.info("耗时:{}:{}", response.body(), (l2 - l1));
                }
            }
        }
    }
}
